﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZSZ.CommonMVC;
using ZSZ.DTO;
using ZSZ.IService;
using ZSZ.Model;
using ZSZ.Website.Areas.WebAdmin.Filters;
using ZSZ.Website.Areas.WebAdmin.Models;

namespace ZSZ.Website.Areas.WebAdmin.Controllers
{
    /// <summary>
    /// 区域
    /// </summary>
    [AdminAuthorize(IsCheck = true)]
    public class RegionController : Controller
    {
        
        public IRegionService regionService { get; set; }
        public ICityService cityService { get; set; }

        // GET: WebAdmin/Region
        public ActionResult List(RegionSearchOptions options, int pageIndex = 1)
        {
            //获取所有城市
            ViewBag.allCities = cityService.GetAll().ToArray();
            long total;
            int pageSize = 10, maxPagerCount = 10;
            List<RegionDTO> list = regionService.GetPagedData(options, out total, pageIndex, pageSize);
            ViewBag.totalCount = total;
            ViewBag.pageIndex = pageIndex;
            ViewBag.pageSize = pageSize;
            ViewBag.maxPagerCount = maxPagerCount;
            return View(list);
        }

        [HttpGet]
        public ActionResult Add()
        {
            var cities = cityService.GetCities();//所有可用的城市
            return View(cities);
        }

        [HttpPost]
        public ActionResult Add(string name,long selectCity)
        {
            if (string.IsNullOrEmpty(name))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "区域名称不能为空" });
            }
            var city = cityService.GetById(selectCity);
            if (city == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "城市不存在" });
            }

            //新增区域
            regionService.Add(name,selectCity);

            return Json(new AjaxResult { Status = "ok" });
        }


        [HttpGet]
        public ActionResult Edit(long id)
        {
            RegionDTO entity = regionService.GetDTOById(id);
            if (entity == null)
            {
                return View("Error", (object)"获取不到区域信息");
            }
            CityDTO[] allCities = cityService.GetCities().ToArray();
            RegionEditGetViewModel model = new RegionEditGetViewModel();
            model.Region = entity;
            model.AllCities = allCities;
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(long id,string name, long selectCity)
        {
            Region entity = regionService.GetById(id);
            if (entity == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "获取不到区域信息" });
            }
            if (string.IsNullOrEmpty(name))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "区域名称不能为空" });
            }
            var city = cityService.GetById(selectCity);
            if (city == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "城市不存在" });
            }

            //更新区域
            regionService.Update(entity, name, selectCity);

            return Json(new AjaxResult { Status = "ok" });
        }


        [HttpPost]
        public ActionResult Delete(long id)
        {
            RegionDTO entity = regionService.GetDTOById(id);
            if (entity == null)
            {
                return View("Error", (object)"获取不到区域信息");
            }
            regionService.MarkDeleted(id);
            return Json(new AjaxResult { Status = "ok" });
        }

        [HttpPost]
        public ActionResult BatchDelete(long[] selectdIds)
        {
            List<long> list = new List<long>();
            foreach (long id in selectdIds)
            {
                list.Add(id);
            }
            regionService.BatchMarkDeleted(list);
            return Json(new AjaxResult { Status = "ok" });
        }
    }
}